package com.song.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.song.api.BaseService;
import com.song.article.mapper.ArticleMapper;
import com.song.article.service.ArticlePortalService;
import com.song.enums.ArticleReviewStatus;
import com.song.enums.YesOrNo;
import com.song.pojo.Article;
import com.song.pojo.vo.ArticleDetailVO;
import com.song.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ArticlePortalServiceImpl extends BaseService implements ArticlePortalService {

    private static final Logger LOG = LoggerFactory.getLogger(ArticlePortalServiceImpl.class);

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 首页查询文章列表
     *
     * @param keyword
     * @param category
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PagedGridResult queryPortalArticleList(String keyword,
                                                  Integer category,
                                                  Integer page,
                                                  Integer pageSize) {

        Example example = new Example(Article.class);
        Example.Criteria criteria = setDefaultArticleExample(example);

        // category 为空则查询全部，不指定分类
        // keyword 为空则查询全部
        if (StringUtils.isNotBlank(keyword)) {
            criteria.andLike("title", "%" + keyword + "%");
        }
        if (category != null) {
            criteria.andEqualTo("categoryId", category);
        }


        /**
         * page: 第几页
         * pageSize: 每页显示条数
         */
        PageHelper.startPage(page, pageSize);
        List<Article> list = articleMapper.selectByExample(example);
        return setPagedGridResult(list, page);

    }

    /**
     * 查询首页热闻列表
     *
     * @return
     */
    @Override
    public List<Article> queryHotList() {

        Example example = new Example(Article.class);
        Example.Criteria criteria = setDefaultArticleExample(example);

        PageHelper.startPage(1, 5);
        List<Article> list = articleMapper.selectByExample(example);
        return list;
    }

    /**
     * 查询创造者文章列表
     *
     * @param writerId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PagedGridResult queryPublisherArticleList(String writerId, Integer page, Integer pageSize) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = setDefaultArticleExample(example);
        criteria.andEqualTo("publishUserId",writerId);

        PageHelper.startPage(page, pageSize);
        List<Article> list = articleMapper.selectByExample(example);
        return setPagedGridResult(list, page);
    }

    /**
     * 查询创作者近期佳文
     *
     * @param writerId
     * @return
     */
    @Override
    public PagedGridResult queryPublisherGoodArticleList(String writerId) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = setDefaultArticleExample(example);
        criteria.andEqualTo("publishUserId",writerId);

        PageHelper.startPage(1, 5);
        List<Article> list = articleMapper.selectByExample(example);
        return setPagedGridResult(list,5);
    }

    /**
     * 查询文章详情
     *
     * @param articleId
     * @return
     */
    @Override
    public ArticleDetailVO queryArticleDetail(String articleId) {

        Article article = new Article();
        article.setId(articleId);
        article.setIsAppoint(YesOrNo.NO.type);
        article.setIsDelete(YesOrNo.NO.type);
        article.setArticleStatus(ArticleReviewStatus.SUCCESS.type);
        article = articleMapper.selectOne(article);

        ArticleDetailVO articleDetailVO = new ArticleDetailVO();
        BeanUtils.copyProperties(article,articleDetailVO);
        articleDetailVO.setCover(article.getArticleCover());

        return articleDetailVO;
    }

    private Example.Criteria setDefaultArticleExample(Example example){
        example.orderBy("publishTime").desc();

        /**
         * 自带隐性查询条件：
         * isPoint为即时发布，表示文章已经直接发布，或者定时任务到点发布
         * isDelete为未删除，表示文章不能展示已经被删除的
         * status为审核通过，表示文章经过机审/人审通过
         */
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isAppoint", YesOrNo.NO.type);
        criteria.andEqualTo("isDelete", YesOrNo.NO.type);
        criteria.andEqualTo("articleStatus", ArticleReviewStatus.SUCCESS.type);

        return criteria;
    }

}